package com.example.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class GpioAdapter extends BaseAdapter {
    private Context context;
    private List<Gpio_item> items;

    public GpioAdapter(Context context) {
        this.context = context;
        this.items = new ArrayList<>();
    }

    public void addItem() {
        items.add(new Gpio_item());
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Gpio_item getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.gpio_test, parent, false);
            holder = new ViewHolder();
            holder.gpioNumberInput = convertView.findViewById(R.id.gpioNumberInput);
            holder.directionGroup = convertView.findViewById(R.id.directionGroup);
            holder.valueGroup = convertView.findViewById(R.id.valueGroup);
            holder.setButton = convertView.findViewById(R.id.setButton);
            holder.readValueButton = convertView.findViewById(R.id.readValueButton);
            holder.resultText = convertView.findViewById(R.id.resultText);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Gpio_item item = items.get(position);

        holder.setButton.setOnClickListener(v -> {
            String gpioStr = holder.gpioNumberInput.getText().toString();
            if (gpioStr.isEmpty()) {
                Toast.makeText(context, "请输入GPIO编号", Toast.LENGTH_SHORT).show();
                return;
            }

            int gpioNum = Integer.parseInt(gpioStr);
            boolean isOutput = holder.directionGroup.getCheckedRadioButtonId() == R.id.directionOut;
            boolean isHigh = holder.valueGroup.getCheckedRadioButtonId() == R.id.valueHigh;

            if (isOutput)
            {
                ((MainActivity) context).setGpioValue(gpioNum, isHigh ? 1 : 0);
                String result = "GPIO " + gpioNum + " 已设置为" + (isHigh ? "高" : "低") + "电平";
                holder.resultText.setText(result);
                item.setResult(result);
            }
            else {
                ((MainActivity) context).getGpioValue(gpioNum);//设置为输入模式
                String result = "GPIO " + gpioNum + " 设置为输入模式";
                holder.resultText.setText(result);
                item.setResult(result);
            }

        });

        holder.readValueButton.setOnClickListener(v -> {
            String gpioStr = holder.gpioNumberInput.getText().toString();
            if (gpioStr.isEmpty()) {
                Toast.makeText(context, "请输入GPIO编号", Toast.LENGTH_SHORT).show();
                return;
            }
            int gpioNum = Integer.parseInt(gpioStr);
            int value = ((MainActivity) context).getGpioValue(gpioNum);
            String result = "GPIO " + gpioNum + " 当前值: " + value;
            holder.resultText.setText(result);
            item.setResult(result);
        });
        // 恢复保存的状态
        if (item.getResult() != null) {
            holder.resultText.setText(item.getResult());
        }

        return convertView;
    }

    private static class ViewHolder {//控件的实例都存放在 ViewHolder
        EditText gpioNumberInput;
        RadioGroup directionGroup;
        RadioGroup valueGroup;
        Button setButton;
        Button readValueButton;
        TextView resultText;
    }
} 